﻿using AutoMapper;
using Microsoft.AspNetCore.SignalR;
using TrackSystem.Contracts;
using TrackSystem.Entities.Dtos.Equip;
using TrackSystem.Entities.Dtos.Logic;
using TrackSystem.Entities.Models;
using TrackSystem.Entities.RequestFeatures;

namespace TrackSystem.API.SignalRHub
{
    /// <summary>
    /// 基于SignalR的逻辑表达式处理Hub
    /// </summary>
    /// <param name="wrapper"></param>
    /// <param name="mapper"></param>
    public class LogicHelperHub(IRepositoryWrapper wrapper, IMapper mapper): Hub
    {

        /// <summary>
        /// 获取所有逻辑表达式
        /// </summary>
        /// <returns></returns>
        public async Task GetAllAsync()
        {
            var entities = await wrapper.Logic.GetLogicWithEquipAsync();
            var dtos = mapper.Map<IEnumerable<LogicDto>>(entities);
            await Clients.Caller.SendAsync("ReceiveAllLogic", dtos);
        }

        /// <summary>
        /// 获得单个逻辑表达式
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task GetSingleAsync(int id)
        {
            var entities = await wrapper.Logic.GetSingle(id);
            var dto = mapper.Map<LogicDto>(entities);
            await Clients.Caller.SendAsync("RecvSingle", dto);
        }
        /// <summary>
        /// 分页查询逻辑表达式
        /// </summary>
        /// <param name="parameter">分页参数</param>
        /// <returns></returns>
        public async Task GetPagedLogics(RequestParameter parameter)
        {
            var entities = wrapper.Logic.GetPagedLogics(parameter);
            var dtos = mapper.Map<IEnumerable<LogicDto>>(entities);
            await Clients.Caller.SendAsync("ReceivePagedLogics", dtos, entities.MetaData.CurrentPage, entities.MetaData.TotalPages);
        }

        /// <summary>
        /// 获取逻辑表达式的最大Id
        /// </summary>
        /// <returns></returns>
        public async Task GetMaxId()
        {
            var vars = await wrapper.Logic.GetAllListAsync();
            var max = vars.Any() ? vars.Max(a => a.Id) : 0;
            await Clients.Caller.SendAsync("RecvMaxId", max);
        }

        /// <summary>
        /// 更新逻辑表达式
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task UpdateLogicAsync(LogicDto dto)
        {
            var item = mapper.Map<LogicTable>(dto);
            var entities = await wrapper.Logic.UpdateLogicAsync(item);
            if (entities.Success)
            {
                await wrapper.Var.UpdateBaseLogic(mapper.Map<LogicTable>(entities.Data));
            }
            await Clients.Caller.SendAsync("RecvUpdateLogic", entities);
        }

        /// <summary>
        /// 新增逻辑表达式
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task InsertLogicAsync(LogicDto dto)
        {
            var item = mapper.Map<LogicTable>(dto);
            var entities = await wrapper.Logic.InsertLogicAsync(item);
            if (entities.Success)
            {
                await wrapper.Var.UpdateBaseLogic(mapper.Map<LogicTable>(entities.Data));
            }
            await Clients.Caller.SendAsync("RecvInsertLogic", entities);
        }

        /// <summary>
        /// 获取设备列表
        /// </summary>
        /// <returns></returns>
        public async Task GetEquipsAsync()
        {
            var entities = wrapper.Equip.GetAll();
            var equipDtos = mapper.Map<IEnumerable<EquipDto>>(entities);
            await Clients.Caller.SendAsync("RecvEquips", equipDtos);
        }

        /// <summary>
        /// 删除逻辑表达式
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task DeleteLogicAsync(int id)
        {
            var result = await wrapper.Logic.DeleteLogicAsync(id);
            if (result.Success)
            {
                await wrapper.Var.DeleteBaseLogic(id);
            }
            await Clients.Caller.SendAsync("RecvDeleteLogic", result);
        }

        /// <summary>
        /// 获取所有的变量名称
        /// </summary>
        /// <returns></returns>
        public async Task GetVarNames()
        {
            var regs = await wrapper.Var.GetAllListAsync();
            List<string> names = (from reg in regs 
                let c = reg.ReadOnly ? 'R' : 'W' 
                select $"{reg.Name} : {reg.DataType.ToString()[0]}{c}")
                .ToList();
            await Clients.Caller.SendAsync("RecvVarNames", names);
        }
    }
}

